<?php

namespace App\Http\Controllers\Api\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Api\Admin\BaseController;
use App\Models\Namecard;
use App\Models\Article;

class FileUploadController extends BaseController
{
    private $return = array(
        'code' => 1,
        'msg' => ''
    );

    /**
     * 文件上传
     * @param  Request  $request  [description]
     * @param  Namecard $namecard [description]
     * @param  Namecard $namecard [description]
     * @return [type]             [description]
     */
    public function store(Request $request){
        if (!$request->hasFile('tempFile')) {
            $this->return['msg'] = '未获取到上传文件';
            return response()->json($this->return);
        }
        // 取到文件
        $file = $request->file('tempFile');
        if ($file->getSize()>10*1024*1000) {
            $this->return['msg'] = '上传文件不能大于10M';
            return response()->json($this->return);
        }
        // 声明路径名
        $destinationPath = 'uploads/temp/';
        // 生成数据库文件名称
        $file_name = uniqid() .'.'. $file->getClientOriginalExtension();
        // 执行move方法
        if (!$file->move($destinationPath, $file_name)) {
            $this->return['msg'] = '上传失败';
            return response()->json($this->return);
        }else{
            $this->return = array(
                'code' => '0',
                'msg' => '上传成功！',
                'path' => $destinationPath.$file_name
            );
            return response()->json($this->return);
        }
    }
}
